package it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyMenuBar;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;

import java.util.List;


abstract public class FMenuBar extends MenuBar {

    boolean subMenu = false;

    public FMenuBar(boolean bool) {
        super(bool);
        subMenu = true;
    }

    public FMenuBar() {
        super();
    }

    public void onBrowserEvent(Event event) {

        super.onBrowserEvent(event);
        int x = DOM.eventGetClientX(event);
        int y = DOM.eventGetClientY(event);
        if (DOM.eventGetType(event) == Event.ONCLICK) {
            if (!subMenu) {
                List elements = getItems();
                for (int i = 0; i < elements.size(); i++) {
                    MenuItem item = (MenuItem) elements.get(i);
                    int offsetLeft = Window.getScrollLeft();
                    int offsetTop = Window.getScrollTop();
                    if (item.getAbsoluteLeft() + offsetLeft < x && item.getAbsoluteLeft() + item.getOffsetWidth() + offsetLeft >= x)
                        if (item.getAbsoluteTop() + offsetTop < y && item.getAbsoluteTop() + item.getOffsetHeight() + offsetTop >= y)
                            itemClicked(item);
                }
            } else
                itemClicked(getSelectedItem());
        }
    }

    public List getItems() {
        return super.getItems();
    }

    abstract public void itemClicked(MenuItem item);
}
